﻿package mortal.game.control
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import flash.utils.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.cd.*;
    import mortal.game.view.skill.*;
    import mortal.mvc.core.*;

    public class CDController extends Controller
    {
        public static const skillCDPre:int = 250;

        public function CDController()
        {
            return;
        }// end function

        override protected function initServer() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.RuneList, this.runeListHandler);
            NetDispatcher.addCmdListener(ServerCommand.RuneAdd, this.runeListHandler);
            NetDispatcher.addCmdListener(ServerCommand.RuneRemove, this.runeListHandler);
            NetDispatcher.addCmdListener(ServerCommand.RuneLevelUp, this.runeListHandler);
            NetDispatcher.addCmdListener(ServerCommand.SkillListUpdate, this.skillListUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.SkillAdd, this.skillAddHandler);
            NetDispatcher.addCmdListener(ServerCommand.SkillSeriesItemAdd, this.addSkillSeriesItem);
            NetDispatcher.addCmdListener(ServerCommand.SkillRemove, this.skillRemoveHandler);
            NetDispatcher.addCmdListener(ServerCommand.SkillUpdate, this.skillUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.SkillUpgrade, this.skillUpgradeHandler);
            NetDispatcher.addCmdListener(ServerCommand.SkillExListCdUpdate, this.onSkillExListCdUpdate);
            NetDispatcher.addCmdListener(ServerCommand.RoleStaminaUpdate, this.staminaUpdateHandler);
            Dispatcher.addEventListener(EventName.RoleJump, this.jumpHandler);
            Dispatcher.addEventListener(EventName.ScenePlaySkill, this.skillUseSucessHandler);
            Dispatcher.addEventListener(EventName.SkillExCDRemoveAll, this.onExSkillCDRemoveAll);
            NetDispatcher.addCmdListener(ServerCommand.BackpackAllDataGet, this.itemListUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemAdd, this.addItemCDHandler);
            NetDispatcher.addCmdListener(ServerCommand.SkillSeriesItemAdd, this.addSkillSeriesItem);
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemDel, this.delItemCDHandler);
            NetDispatcher.addCmdListener(ServerCommand.ItemCDTime, this.setItemCDHandler);
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsReduction, this.useItemSucessHandler);
            Dispatcher.addEventListener(EventName.SkillClearAllCD, this.clearSkillCDHandler);
            return;
        }// end function

        public function registerBasicSkillCD() : void
        {
            var _loc_2:ICDData = null;
            var _loc_1:* = cache.skill.getSkill(GameConst.JPSkillId);
            if (_loc_1 != null)
            {
                _loc_2 = cache.cd.registerCDData(CDDataType.skillInfo, _loc_1.tSkill.skillId.toString());
                _loc_1.shengeAddCD = cache.buff.getBuffTypeValue(EBuffType._EBuffTypeIncOrDeSprintCd) * 1000;
                _loc_2.totalTime = _loc_1.cooldownTime;
            }
            _loc_1 = cache.skill.getSkill(GameConst.RollingSkillId);
            if (_loc_1 != null)
            {
                _loc_2 = cache.cd.registerCDData(CDDataType.skillInfo, _loc_1.tSkill.skillId.toString());
                _loc_1.shengeAddCD = cache.buff.getBuffTypeValue(EBuffType._EBuffTypeIncOrDeJumpCd) * 1000;
                _loc_2.totalTime = _loc_1.cooldownTime;
            }
            return;
        }// end function

        private function jumpHandler(param1 = null) : void
        {
            cache.skill.setWaittingForServer(GameConst.RollingSkillId, false);
            var _loc_2:* = cache.cd.getCDDataByKeyType(CDDataType.skillInfo, GameConst.RollingSkillId.toString());
            cache.skill.isLastUseRolling = false;
            if (_loc_2 != null)
            {
                _loc_2.startCoolDown();
            }
            return;
        }// end function

        private function staminaUpdateHandler(param1:Object) : void
        {
            var _loc_3:ICDData = null;
            var _loc_2:* = int(param1);
            if (_loc_2 < 0)
            {
                if (cache.skill.isLastUseRolling)
                {
                }
                else
                {
                    cache.skill.setWaittingForServer(GameConst.JPSkillId, false);
                    _loc_3 = cache.cd.getCDDataByKeyType(CDDataType.skillInfo, GameConst.JPSkillId.toString());
                }
                if (_loc_3 != null)
                {
                    _loc_3.startCoolDown();
                }
            }
            return;
        }// end function

        private function clearSkillCDHandler(event:DataEvent) : void
        {
            this.clearAllSkillCd();
            return;
        }// end function

        private function itemListUpdateHandler(param1:Object) : void
        {
            var _loc_3:ItemData = null;
            var _loc_2:* = cache.pack.backPackCache.getAllItems();
            if (_loc_2 == null)
            {
                return;
            }
            for each (_loc_3 in _loc_2)
            {
                
                if (_loc_3 == null)
                {
                    return;
                }
                this.addItemCDHandler(_loc_3);
            }
            return;
        }// end function

        private function addItemCDHandler(param1:Object) : void
        {
            var _loc_2:* = param1 as ItemData;
            if (_loc_2.itemInfo.cdTime <= 0)
            {
                return;
            }
            var _loc_3:* = CDCache.getKey(_loc_2.itemInfo);
            var _loc_4:* = cache.cd.registerCDData(CDDataType.itemData, _loc_3);
            _loc_4.totalTime = _loc_2.itemInfo.cdTime * 1000;
            return;
        }// end function

        private function addSkillSeriesItem(param1:Object) : void
        {
            var _loc_2:* = SkillConfig.instance.getInfoById(param1 as int);
            var _loc_3:* = SkillUtil.getSkillSeries(_loc_2.skillId).toString();
            var _loc_4:* = cache.cd.registerCDData(CDDataType.skillSeries, _loc_3);
            _loc_4.totalTime = _loc_2.cooldownTime - skillCDPre;
            if (_loc_4 && !_loc_4.isCoolDown)
            {
                _loc_4.startCoolDown();
            }
            return;
        }// end function

        private function delItemCDHandler(param1:Object) : void
        {
            return;
        }// end function

        private function setItemCDHandler(param1:Object) : void
        {
            return;
        }// end function

        private function useItemSucessHandler(param1:Object) : void
        {
            var _loc_2:* = param1[0] as ItemData;
            var _loc_3:* = int(param1[1]);
            if (_loc_2 == null || _loc_2.itemInfo.cdTime <= 0)
            {
                return;
            }
            if (_loc_3 != UpdateCode.EUpdateCodeBagUseDrug)
            {
                return;
            }
            if (!ItemsUtil.isLefeAndManaAndPetLifeDrug(_loc_2))
            {
                return;
            }
            var _loc_4:* = cache.cd.getCDData(_loc_2, CDDataType.itemData);
            _loc_4.startCoolDown();
            return;
        }// end function

        private function onSkillExListCdUpdate(param1:Object) : void
        {
            this.skillListUpdateHandler(true);
            return;
        }// end function

        private function onExSkillCDRemoveAll(event:DataEvent) : void
        {
            var _loc_2:SkillInfo = null;
            for each (_loc_2 in cache.skill.getExSkillList())
            {
                
                if (_loc_2)
                {
                    cache.cd.unregisterCDData(CDDataType.skillInfo, _loc_2.skillId.toString());
                    this.removeSkillPublicCD(_loc_2.skillId);
                }
            }
            return;
        }// end function

        private function runeListHandler(param1:Object = null) : void
        {
            var _loc_3:SkillInfo = null;
            var _loc_4:ICDData = null;
            var _loc_2:* = cache.skill.getAllSkillsLearned();
            if (_loc_2 == null)
            {
                return;
            }
            for each (_loc_3 in _loc_2)
            {
                
                _loc_4 = cache.cd.getCDData(_loc_3, CDDataType.skillInfo);
                if (_loc_4 != null)
                {
                    _loc_4.totalTime = _loc_3.cooldownTime - skillCDPre;
                }
            }
            return;
        }// end function

        public function registerSkillCD(param1:SkillInfo) : ICDData
        {
            var _loc_4:Array = null;
            var _loc_5:int = 0;
            var _loc_6:TSkill = null;
            var _loc_7:ICDData = null;
            var _loc_2:* = cache.cd.registerCDData(CDDataType.skillInfo, param1.skillId.toString());
            _loc_2.totalTime = param1.cooldownTime - skillCDPre;
            this.addSkillPublicCD(param1, _loc_2);
            var _loc_3:* = param1.tSkill.combo;
            if (_loc_3 != null && _loc_3.length > 2)
            {
                if (_loc_3.indexOf("#") < 0)
                {
                    _loc_4 = [_loc_3];
                }
                else
                {
                    _loc_4 = _loc_3.split("#");
                }
                if (_loc_4 != null && _loc_4.length > 0)
                {
                    _loc_5 = 0;
                    while (_loc_5 < _loc_4.length)
                    {
                        
                        _loc_3 = _loc_4[_loc_5];
                        _loc_6 = SkillConfig.instance.getInfoById(parseInt(_loc_3));
                        if (_loc_6 != null)
                        {
                            _loc_7 = cache.cd.registerCDData(CDDataType.skillInfo, _loc_6.skillId.toString());
                            _loc_7.totalTime = param1.cooldownTime - skillCDPre - param1.tSkill.cooldownTime + _loc_6.cooldownTime;
                            this.addSkillPublicCD(param1, _loc_7);
                        }
                        _loc_5++;
                    }
                }
            }
            return _loc_2;
        }// end function

        private function skillListUpdateHandler(param1:Object = null) : void
        {
            var _loc_3:SkillInfo = null;
            var _loc_4:ICDData = null;
            var _loc_5:int = 0;
            var _loc_2:* = param1 ? (cache.skill.getExSkillList()) : (cache.skill.getAllSkillsLearned());
            if (_loc_2 == null)
            {
                return;
            }
            for each (_loc_3 in _loc_2)
            {
                
                _loc_4 = this.registerSkillCD(_loc_3);
                _loc_5 = _loc_3.cdDt.time - ClockManager.instance.nowDate.time;
                if (_loc_5 > _loc_3.cooldownTime)
                {
                    _loc_5 = 0;
                }
                if (_loc_5 > 500)
                {
                    _loc_4.beginTime = getTimer() - _loc_3.cooldownTime + _loc_5;
                    _loc_4.startCoolDown();
                }
            }
            return;
        }// end function

        private function skillAddHandler(param1:Object) : void
        {
            var _loc_2:* = param1 as SSkill;
            var _loc_3:* = cache.skill.getSkill(_loc_2.skillId);
            if (_loc_3 == null)
            {
                return;
            }
            this.registerSkillCD(_loc_3);
            return;
        }// end function

        private function skillRemoveHandler(param1:Object) : void
        {
            var _loc_2:* = param1 as SSkill;
            var _loc_3:* = cache.skill.getSkill(_loc_2.skillId);
            if (_loc_3 == null)
            {
                return;
            }
            cache.cd.unregisterCDData(CDDataType.skillInfo, _loc_3.skillId.toString());
            this.removeSkillPublicCD(_loc_3.skillId);
            return;
        }// end function

        private function skillUpdateHandler(param1:Object) : void
        {
            var _loc_5:int = 0;
            var _loc_6:int = 0;
            var _loc_2:* = param1 as SSkill;
            var _loc_3:* = cache.skill.getSkill(_loc_2.skillId);
            if (_loc_3 == null)
            {
                return;
            }
            var _loc_4:* = cache.cd.getCDDataByKeyType(CDDataType.skillInfo, _loc_3.skillId.toString());
            if (_loc_4 != null)
            {
                _loc_5 = _loc_2.cdDt.time - ClockManager.instance.nowDate.time;
                if (_loc_5 < 0)
                {
                    return;
                }
                _loc_6 = _loc_4.totalTime - _loc_5;
                _loc_4.beginTime = getTimer() - _loc_6;
                _loc_4.startCoolDown();
            }
            return;
        }// end function

        private function skillUpgradeHandler(param1:Object) : void
        {
            var _loc_2:* = param1 as SSkill;
            var _loc_3:* = cache.skill.getSkill(_loc_2.skillId);
            if (_loc_3 == null)
            {
                return;
            }
            var _loc_4:* = SkillConfig.instance.getSkillByLevel(_loc_3.tSkill.series, (_loc_3.tSkill.skillLevel - 1));
            if (_loc_4 != null)
            {
                cache.cd.unregisterCDData(CDDataType.skillInfo, _loc_4.skillId.toString());
                this.removeSkillPublicCD(_loc_4.skillId);
            }
            this.registerSkillCD(_loc_3);
            return;
        }// end function

        private function addSkillPublicCD(param1:SkillInfo, param2:ICDData) : void
        {
            var _loc_3:* = cache.cd.registerCDData(CDDataType.publicCD, "skillPublicCD") as CDDataPublicCD;
            if (param1.tSkill.affectCdType == ESkillAffectCdType._ESkillAffectCdTypeNormal || param1.tSkill.affectCdType == ESkillAffectCdType._ESkillAffectCdTypeNoAffect)
            {
                if (param1.tSkill.triggerType == ESkillTriggType._ESkillTriggXp)
                {
                    return;
                }
                _loc_3.addPublicCD(param1.skillId, param2);
            }
            return;
        }// end function

        private function removeSkillPublicCD(param1:int) : void
        {
            var _loc_2:* = cache.cd.registerCDData(CDDataType.publicCD, "skillPublicCD") as CDDataPublicCD;
            _loc_2.removePublicCD(param1);
            return;
        }// end function

        private function resetCDToFirstSkill(param1:ICDData, param2:int) : ICDData
        {
            if (param1 == null)
            {
                return null;
            }
            var _loc_3:* = cache.skill.getFirstSkill();
            if (_loc_3 == null || _loc_3.tSkill.combo == null || _loc_3.tSkill.combo.indexOf(param2.toString()) < 0)
            {
                return param1;
            }
            var _loc_4:* = SkillConfig.instance.getInfoById(param2);
            if (_loc_4 == null)
            {
                return param1;
            }
            param1 = cache.cd.getCDDataByKeyType(CDDataType.skillInfo, _loc_3.tSkill.skillId.toString());
            param1.totalTime = _loc_4.cooldownTime + _loc_3.cooldownTime - _loc_3.tSkill.cooldownTime - skillCDPre;
            return param1;
        }// end function

        public function skillUseSucessHandler(event:DataEvent) : void
        {
            var _loc_2:* = int(event.data);
            var _loc_3:* = cache.cd.getCDDataByKeyType(CDDataType.skillInfo, _loc_2.toString());
            _loc_3 = this.resetCDToFirstSkill(_loc_3, _loc_2);
            if (_loc_3 != null)
            {
                _loc_3.startCoolDown();
            }
            var _loc_4:* = SkillConfig.instance.getInfoById(_loc_2);
            if (_loc_4.affectCdType == ESkillAffectCdType._ESkillAffectCdTypeNoCheckNoAffect)
            {
                return;
            }
            if (_loc_4.triggerType == ESkillTriggType._ESkillTriggXp)
            {
                return;
            }
            _loc_3 = cache.cd.registerCDData(CDDataType.publicCD, "skillPublicCD");
            if (_loc_3.isCoolDown)
            {
                return;
            }
            _loc_3.totalTime = _loc_4.publicCdTime == 0 ? (1000) : (_loc_4.publicCdTime - skillCDPre);
            _loc_3.startCoolDown();
            var _loc_5:* = cache.skill.getComboSkill();
            if (_loc_5 != null)
            {
                cache.skill.nextComboSkillIndex = SkillUtil.getComboSkillIndex(_loc_5.tSkill, _loc_2) + 1;
            }
            cache.skill.nextComboSkillIndex = cache.skill.nextComboSkillIndex % 5;
            var _loc_6:* = getTimer();
            var _loc_7:int = 1000;
            var _loc_8:* = SkillUtil.getComboSkillByIndex(_loc_5.tSkill, cache.skill.nextComboSkillIndex);
            if (_loc_8 != null && _loc_8.publicCdTime > 0)
            {
                _loc_7 = _loc_8.publicCdTime;
            }
            cache.skill.backToComboIndex0Time = _loc_6 + _loc_7 + 100;
            return;
        }// end function

        public function clearAllSkillCd() : void
        {
            var _loc_2:ICDData = null;
            var _loc_3:SkillInfo = null;
            var _loc_1:* = cache.skill.getAllSkillsLearned();
            for each (_loc_3 in _loc_1)
            {
                
                if (_loc_3.cooldownTime > 0)
                {
                    _loc_2 = cache.cd.getCDData(_loc_3, CDDataType.skillInfo);
                    if (_loc_2 != null && _loc_2.isCoolDown)
                    {
                        _loc_2.stopCoolDown();
                    }
                }
            }
            return;
        }// end function

    }
}
